package org.iipg.service.core.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.iipg.common.data.dto.PageDto;
import org.iipg.common.data.entity.DomainInfo;
import org.iipg.common.data.mapper.DomainInfoMapper;
import org.iipg.common.data.vo.PhResponse;
import org.iipg.service.core.filter.QueryDnscrackListFilter;
import org.iipg.service.core.filter.QueryDomainNameListFilter;
import org.iipg.service.core.mapper.DomainNameCustomMapper;
import org.iipg.service.core.util.ConvUtil;
import org.iipg.service.core.vo.DnsCrackVo;
import org.iipg.service.core.vo.DomainNameVo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhuhy163@163.com
 * @Description
 * @date 2021/7/16 上午8:56
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(value = "域名查询接口", tags={"域名查询接口"})
@RequestMapping("DomainName")
public class DomainNameController {

    private final DomainInfoMapper domainInfoMapper;

    private final DomainNameCustomMapper domainNameCustomMapper;

    /**
     * 直接使用 MybatisPlus 的 Mapper 示例
     */
    @ApiOperation(value = "查询 DNS Crack 列表", produces = "application/json")
    @RequestMapping(value = "/QueryDnscrackList", method = RequestMethod.POST,
            produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
    public PhResponse<PageDto<DomainInfo>> queryDnscrackList(@RequestBody QueryDnscrackListFilter queryDnscrackListFilter) {
        // 创建分页查询
        IPage<DomainInfo> page = new Page<>(queryDnscrackListFilter.getPageNum(), queryDnscrackListFilter.getPageSize());

        LambdaQueryWrapper<DomainInfo> queryWrapper = Wrappers.<DomainInfo>lambdaQuery();

        IPage<DomainInfo> result = domainInfoMapper.selectPage(page, queryWrapper);

        return PhResponse.ok(ConvUtil.convert(result, DomainInfo.class));
    }

    /**
     * 使用自定义SQL进行查询示例
     */
    @ApiOperation(value = "查询 Domain Name 列表", produces = "application/json")
    @RequestMapping(value = "/QueryDomainNameList", method = RequestMethod.POST,
            produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
    public PhResponse<PageDto<DomainNameVo>> queryDomainNameList(@RequestBody QueryDomainNameListFilter queryDomainNameListFilter) {
        // 创建分页查询
        IPage<DomainNameVo> page = new Page<>(queryDomainNameListFilter.getPageNum(), queryDomainNameListFilter.getPageSize());

        IPage<DomainNameVo> result = domainNameCustomMapper.selectPage(page, queryDomainNameListFilter);

        return PhResponse.ok(ConvUtil.convert(result, DomainNameVo.class));
    }
}
